package cn.iocoder.yudao.module.yyzx.service.outings;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.yyzx.controller.admin.outings.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.outings.OutingsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.yyzx.dal.mysql.outings.OutingsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.yyzx.enums.ErrorCodeConstants.*;

/**
 * 外出管理 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class OutingsServiceImpl implements OutingsService {

    @Resource
    private OutingsMapper outingsMapper;

    @Override
    public Integer createOutings(OutingsSaveReqVO createReqVO) {
        // 插入
        OutingsDO outings = BeanUtils.toBean(createReqVO, OutingsDO.class);
        outingsMapper.insert(outings);
        // 返回
        return outings.getId();
    }

    @Override
    public void updateOutings(OutingsSaveReqVO updateReqVO) {
        // 校验存在
        validateOutingsExists(updateReqVO.getId());
        // 更新
        OutingsDO updateObj = BeanUtils.toBean(updateReqVO, OutingsDO.class);
        outingsMapper.updateById(updateObj);
    }

    @Override
    public void deleteOutings(Integer id) {
        // 校验存在
        validateOutingsExists(id);
        // 删除
        outingsMapper.deleteById(id);
    }

    private void validateOutingsExists(Integer id) {
        if (outingsMapper.selectById(id) == null) {
            throw exception(OUTINGS_NOT_EXISTS);
        }
    }

    @Override
    public OutingsDO getOutings(Integer id) {
        return outingsMapper.selectById(id);
    }

    @Override
    public PageResult<OutingsDO> getOutingsPage(OutingsPageReqVO pageReqVO) {
        return outingsMapper.selectPage(pageReqVO);
    }

}